package com.zjh.menucloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjh.common.R;
import com.zjh.entity.Menu;
import com.zjh.entity.Type;
import com.zjh.menucloud.mapper.MenuMapper;
import com.zjh.menucloud.mapper.TypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: MenuController
 * Description:
 * @Author zjh
 * @Create 2023/9/28 9:17
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
	
	@Autowired
	private MenuMapper mapper;
	
	@Autowired
	private TypeMapper typeMapper;
	
	@GetMapping("/{begin}/{size}")
	@ResponseBody
	public R<List<Menu>> pageList(@PathVariable("begin") Integer begin, @PathVariable("size") Integer size) {
//		List<Menu> all = mapper.findAll(begin,size);
		return new R(0, "", mapper.count(), mapper.findAll(begin, size));
	}
	//	@GetMapping("/mp/{page}/{pagesize}")
//	public IPage<Menu> testMP(@PathVariable("page") Integer page, @PathVariable("pagesize") Integer size) {
//		Page page1=new Page<>(page,size);
//		return mapper.selectPage(page1,null);
//	}
	
	// 删除
	@DeleteMapping("/deleteById/{id}")
	public R deleteById(@PathVariable("id") Integer id) {
		int i = mapper.deleteById(id);
		if (i > 0) {
			return new R<>(200, "删除成功", i, null);
		}
		return new R<>(400, "失败", null, null);
	}
	
	@GetMapping("/findById/{id}")
	public Menu findByIdOne(@PathVariable("id") Integer id) {
		return mapper.findById(id);
	}
	
	@GetMapping("/findAllTypes")
	public List<Type> findAllTypes() {
		List<Type> types = typeMapper.findType();
		return types;
	}
	
	@PostMapping("/update")
	public void updateOne(@RequestBody Menu menu) {
		mapper.update(menu);
	}
	
	@GetMapping("/findTNameById")
	public List<Type> queryNameById(){
		List<Type> types = typeMapper.findType();
		return types;
	}
	
	@PostMapping("/saveMenu")
	public void saveMenu(@RequestBody Menu menu){
		
		mapper.save(menu);
	}
}
